import type {
    Configuration,
    EntryObject,
    WebpackOptionsNormalized,
} from 'webpack'

import HtmlWebpackPlugin from 'html-webpack-plugin'

import 路径工具 from 'path'
const 求绝对路径_操作系统式 = 路径工具.resolve
const 拼接路径_POSIX式 = 路径工具.posix.join





const focusingTests = [
    '测试甲',
    '测试乙',
] as const

type _范_测试源代码之关键文件夹名 = (typeof focusingTests)[number]

function 求某Webpack资源块之名称 (某测试源代码之关键文件夹名: _范_测试源代码之关键文件夹名, 类别: 'js' | 'html'): string {
    return `${某测试源代码之关键文件夹名}:${类别}`
}





const webpackEntries集: EntryObject = focusingTests.reduce(
    (_webpackEntries集, 某测试源代码之关键文件夹名) => {
        const webpack资源块之名 = 求某Webpack资源块之名称(某测试源代码之关键文件夹名, 'js')
        console.debug(`Webpack资源块之名： "${webpack资源块之名}"`)

        const webpack单个资源块: EntryObject[string] = {
            import: 求绝对路径_操作系统式('源', '代码', '诸测试', 某测试源代码之关键文件夹名, 'index.ts'),
            filename: 拼接路径_POSIX式(某测试源代码之关键文件夹名, 'index.js'),
            // publicPath: 拼接路径_POSIX式(encodeURIComponent(某测试源代码之关键文件夹名)),
        }

        _webpackEntries集[webpack资源块之名] = webpack单个资源块

        return _webpackEntries集
    },
    {} as EntryObject
)

// console.debug('webpackEntries集', webpackEntries集)





const webpackConfig: Configuration | WebpackOptionsNormalized = {
    mode: 'development',

    devServer: {
        port: 3317,
        hot: false,
        allowedHosts: 'all',
    },

    watchOptions: {
        ignored: [
            '**/node_modules',
        ],
    },

    resolve: {
        extensions: [ '.ts', '.js', '.cjs', '.mjs' ],
    },

    entry: webpackEntries集,

    output: {
        path: 求绝对路径_操作系统式('产出物'),
        clean: true,
    },

    plugins: [
        ...focusingTests.map(某测试源代码之关键文件夹名 => {
            return new HtmlWebpackPlugin({
                template: 求绝对路径_操作系统式('源', '代码', '诸测试', 某测试源代码之关键文件夹名, 'index.html'),
                filename: 拼接路径_POSIX式(某测试源代码之关键文件夹名, 'index.html'),
                // publicPath: 拼接路径_POSIX式(encodeURIComponent(某测试源代码之关键文件夹名)),
                // publicPath: '/',
                minify: {
                    removeComments: true,
                },
                chunks: [
                    求某Webpack资源块之名称(某测试源代码之关键文件夹名, 'js'),
                ],
                inject: 'body',

            })
        }),
    ].filter(插件 => !!插件),

    module: {
        rules: [
            {
                test: /\.(scss|sass)$/i,
                use: [
                    'style-loader',
                    'css-loader',
                    'sass-loader',
                ],
            },

            {
                test: /\.ts$/i,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ].filter(规则 => !!规则),
    },
}

export default webpackConfig
